﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NetCore.Common.Helper
{
    /// <summary>
    /// appsettings.json操作类
    /// </summary>
    public class AppSettings
    {
        #region redis

        /// <summary>
        /// Redis主库连接字符串
        /// </summary>
        public static string Main_RedisConfiguration { get; set; }

        /// <summary>
        /// Redis从库连接字符串
        /// </summary>
        public static List<string> Salve_RedisConfiguration = new List<string>();

        /// <summary>
        /// 连接字符串是否加密
        /// </summary>
        public static bool RedisEncrypted_ConnectionString { get; set; }

        /// <summary>
        /// 连接字符串加密Key
        /// </summary>
        public static string RedisConnectionStringKey { get; set; }

        /// <summary>
        /// 连接字符串加密VI
        /// </summary>
        public static string RedisConnectionStringVI { get; set; }

        /// <summary>
        /// Redis是否启用
        /// </summary>
        public static bool RedisEnabled { get; set; }

        #endregion redis

        /// <summary>
        /// log4net 仓储库
        /// </summary>
        //public static ILoggerRepository repository { get; set; }
        private static IConfiguration Configuration { get; set; }

        public static string ContentPath { get; set; }

        /// <summary>
        /// 请求参数是否加密
        /// </summary>
        public static bool Parameter { get; set; }

        /// <summary>
        /// 请求参数加密Key
        /// </summary>
        public static string ParameterKey { get; set; }

        /// <summary>
        /// 请求参数加密VI
        /// </summary>
        public static string ParameterVI { get; set; }

        /// <summary>
        /// 是否任务调度Quartz.Net
        /// </summary>
        public static bool Encrypted_TaskScheduling { get; set; }

        /// <summary>
        /// 每隔多少秒执行一次任务
        /// </summary>
        public static int Interval_TaskScheduling { get; set; }

        /// <summary>
        /// 发送者邮箱
        /// </summary>
        public static string FromPerson { get; set; }

        /// <summary>
        /// 客户端授权码(可存在配置文件中)
        /// </summary>
        public static string Code { get; set; }

        public AppSettings(string contentPath)
        {
            string Path = "appsettings.json";

            //如果你把配置文件 是 根据环境变量来分开了，可以这样写
            //Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";

            Configuration = new ConfigurationBuilder()
               .SetBasePath(contentPath)
               .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//这样的话，可以直接读目录里的json文件，而不是 bin 文件夹下的，所以不用修改复制属性
               .Build();
        }

        public AppSettings(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string App(params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception)
            {
            }

            return "";
        }

        /// <summary>
        /// 递归获取配置信息数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static List<T> App<T>(params string[] sections)
        {
            List<T> list = new List<T>();
            Configuration.Bind(string.Join(":", sections), list);
            return list;
        }
    }
}